package org.crumbleworks.crumbchat.server.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.crumbleworks.crumbchat.server.controller.GuiController;

public class PortPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private JLabel tcpPortFieldLabel, udpPortFieldLabel;
    private JTextField tcpPortField, udpPortField;
    private JButton bindButton, unbdindButton;

    private GuiController controller;

    public PortPanel(GuiController controller) {
        this.controller = controller;

        setLayout(new GridBagLayout());

        initComponents();
        placeComponents();

        setVisible(true);
    }

    private void initComponents() {
        tcpPortFieldLabel = new JLabel("UDP-Port:");
        udpPortFieldLabel = new JLabel("TCP-Port:");

        tcpPortField = new JTextField(10);
        udpPortField = new JTextField(10);

        bindButton = new JButton("bind");
        bindButton.addActionListener(this);

        unbdindButton = new JButton("unbind");
        unbdindButton.addActionListener(this);
    }

    private void placeComponents() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        // TcpPort
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(tcpPortFieldLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(tcpPortField, gbc);

        // UdpPort
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(udpPortFieldLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(udpPortField, gbc);

        // BindButton
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(bindButton, gbc);

        // UnbindButton
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(unbdindButton, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource().equals(bindButton)) {
            if (tcpPortField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Bitte geben sie einen TCP-Port an.", "TCP-Port angeben",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else if (udpPortField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Bitte geben sie einen UDP-Port an.", "UDP-Port angeben",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                controller.bindPorts(Integer.parseInt(tcpPortField.getText()), Integer.parseInt(udpPortField.getText()));
            }
        }
        else if (ae.getSource().equals(unbdindButton)) {
            controller.unbindPorts(false);
        }
    }
}
